<?php

namespace app\admin\controller;

use app\admin\model\Manager as ManagerModel;
use app\BaseController;
use app\lib\tool\Str;
use think\facade\Cache;
use think\captcha\facade\Captcha;

class Manager extends BaseController
{
    /* 验证码 */
    public function captcha()
    {
        return Captcha::create();
    }

    /* 登录 */
    public function login()
    {
        $username = input('username', '');
        $password = input('password', '');
//        $captcha = input('captcha', '');
//
//        $verify = Captcha::check($captcha);
//        if (!$verify) {
//            return show(config('status.error_login_captcha'), '验证码错误，请重新尝试');
//        }

        if ($username != 'admin' || $password != 'admin') {
            return show(config('status.error_login_status'), '帐号密码不正确');
        }
        $token = Str::getToken();
        $redisData = [
            'id' => 1,
            'cid' => 0,
            'username' => 'admin',
            'main' => 'main',
        ];
        Cache::set(config('cache.redis.prefix') . $token, $redisData, config('cache.redis.timeout'));
        $data = ['token' => $token, 'cid' => 0, 'nickname' => 'admin', 'main' => 0];
        return show(config('status.success'), '登录成功', $data);

        $result = ManagerModel::where(['username' => $username])->find();
        if ($result) {
            if ($result['main'] == 0 && $result['status'] == 0) {
                return show(config('status.error_login_status'), '账号已被冻结，请联系超级管理员');
            }

            $dbPwd = md5(md5($password) . $result['extra']);
            if ($dbPwd == $result['password']) {
                $token = Str::getToken();
                $redisData = [
                    'id' => $result['id'],
                    'cid' => $result['cid'],
                    'username' => $result['username'],
                    'main' => $result['main'],
                ];
                Cache::set(config('cache.redis.prefix') . $token, $redisData, config('cache.redis.timeout'));

                $data = ['token' => $token, 'cid' => 0, 'nickname' => 'admin', 'main' => 0];
                return show(config('status.success'), '登录成功', $data);
            } else {
                return show(config('status.error_login_username'), '登录密码错误，请重新尝试');
            }
        } else {
            return show(config('status.error_login_password'), '登录账号错误，请重新尝试');
        }
    }

    /* 退出登录 */
    public function logout()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        Cache::delete(config('cache.redis.prefix') . $this->request->token);
        return show(config('status.success'), '退出登录');
    }

    /* 列表 */
    public function index()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $page = input('page', 1, 'intval');
        $perpage = 10;
        $result = ManagerModel::order('id asc')->page($page, $perpage)->where('cid', $cid)->select();
        $total = ManagerModel::where('cid', $cid)->count();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(config('status.success'), '获取列表', $data);
    }

    /* 添加 */
    public function add()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $username = input('username', '');
        $password = input('password', '');
        $extra = mt_rand(100000, 999999);
        $nickname = input('nickname', '');
        $status = input('status', 0, 'intval');
        $password = md5(md5($password) . $extra);

        $result = ManagerModel::create([
            'cid' => $cid,
            'username' => $username,
            'password' => $password,
            'extra' => $extra,
            'nickname' => $nickname,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '添加成功');
        } else {
            return show(config('status.error_insert'), '添加失败');
        }
    }

    /* 获取 */
    public function get()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $cid = input('cid', 0, 'intval');
        $data = ManagerModel::where(['id' => $id, 'cid' => $cid])->find();
        if ($data) {
            return show(config('status.success'), '查询成功', $data);
        } else {
            return show(config('status.error_find'), '查询失败');
        }
    }

    /* 修改 */
    public function modify()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $username = input('username', '');
        $password = input('password', '');
        $extra = mt_rand(100000, 999999);
        $nickname = input('nickname', '');
        $status = input('status', 0, 'intval');
        $password = md5(md5($password) . $extra);

        $result = ManagerModel::update([
            'id' => $id,
            'username' => $username,
            'password' => $password,
            'extra' => $extra,
            'nickname' => $nickname,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }

    /* 切换 */
    public function switch()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $status = input('status', 0, 'intval');

        $result = ManagerModel::update([
            'id' => $id,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '状态切换成功');
        } else {
            return show(config('status.error_switch'), '状态切换失败');
        }
    }

    /* 删除 */
    public function delete()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $result = ManagerModel::destroy($id);
        if ($result) {
            return show(config('status.success'), '删除成功');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }
}
